using System;
using System.Text;
using InfoJetSoft.Service.XPath.Tokens;

//CID 0164 //2008.12.13 //Begin
namespace InfoJetSoft.Service.XPath.Exprs
{
	/// <summary>
	/// Summary description for NodeType.
	/// </summary>
    public class NodeType : IExpr
	{
		private string _type = null;

		public NodeType()
		{
			//
			// TODO: Add constructor logic here
			//
        }

        public object Evaluate(ExprContext context)
        {
            return context.Calculator.NodeType(this._type);
        }

        //CID 0866 //2011.04.01 //Begin
        public void Visit(IExprVisitor visitor)
        {
            visitor.SetExplicit(false);
        }
        //CID 0866 //2011.04.01 //End

        public void Print(StringBuilder buffer)
        {
            buffer.Append(this._type + "()");
        }

		public static NodeType Parse( ExprParser parser )
		{
			Token nextToken = parser.LookupNextToken(1);
			if( nextToken == null )
			{
				return null;
			}
			if( nextToken.Type.Equals( TokenType.NCName ) && ( nextToken.Value.Equals( "node" ) || nextToken.Value.Equals( "text" ) || nextToken.Value.Equals( "comment" ) ) )
			{
				Token ntLeftParen = parser.LookupNextToken(2);
				Token ntRightParen = parser.LookupNextToken(3);
				if( ntLeftParen == null || ntRightParen == null )
				{
					return null;
				}
				if( ntLeftParen.Type.Equals( TokenType.Symbol ) && ntLeftParen.Value.Equals( "(" )
					&& ntRightParen.Type.Equals( TokenType.Symbol ) && ntRightParen.Value.Equals( ")" ) )
				{
					parser.MoveNextIndex(3);
					NodeType nodeType = new NodeType();
					nodeType._type = nextToken.Value;
					return nodeType;
				}
			}
			return null;
		}
	}
}
//CID 0164 //2008.12.13 //End
